package com.antball.config;

import com.antball.model.Book;
import com.antball.model.User;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
 * @Auther: huangsj
 * @Date: 2019/7/18 16:31
 * @Description:
 */
public class BookBeanDefinitionParse  extends AbstractSingleBeanDefinitionParser {

    private static final String ARGUMENTS = "book-arguments";
    private static final String BOOKNAME_ATTRIBUTE = "bookName";
    private static final String ISFREE_ATTRIBUTE = "isFree";


    /**
     * 加上这个可以不用设置id属性
     * @return
     */
    @Override
    protected boolean shouldGenerateIdAsFallback() {
        return true;
    }

    @Override
    protected Class<?> getBeanClass(Element element) {
        return Book.class;
    }

    @Override
    protected void doParse(Element element, BeanDefinitionBuilder builder) {

        NamespaceUtils.addConstructorArgValueIfAttributeDefined(builder, element, BOOKNAME_ATTRIBUTE);

        NamespaceUtils.addConstructorArgBooleanValueIfAttributeDefined(builder, element, ISFREE_ATTRIBUTE, false);


        String arguments = element.getAttribute(ARGUMENTS);

//        if (StringUtils.hasText(arguments)) {
//            builder.addConstructorArgReference(arguments);
//        }


    }
}
